<?php

namespace App\Controller;

use App\Entity\Product;
use App\Repository\ProductRepository;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class ProductController
 * @package App\Controller
 * @Route("/product")
 */
class ProductController extends AbstractController
{
    /**
     * @Route("/", name="product")
     */
    public function index()
    {
        return $this->render('product/index.html.twig', [
            'controller_name' => 'ProductController',
        ]);
    }

    /**
     * @Route("/list")
     * @return Response
     */
    public function list()
    {
        $em = $this->getDoctrine()->getManager();


        $repository = $em->getRepository(Product::class);
//
        $em->getFilters()->disable("softdeleteable");
        dump($repository->findAll());

        return new Response('<html><body></body></html>');
    }

    /**
     * @Route("/{id}", requirements={"id":"\d+"})
     * @param int $id
     * @param ProductRepository $productRepository
     * @return Response
     */
    public function info(int $id, ProductRepository $productRepository)
    {
        $em = $this->getDoctrine()->getManager();
        dump($em->getFilters());
        $product = $productRepository->findOneBy(['id'=>$id]);
        dump($product);

        $em->getFilters()->disable('softdeleteable');
        dump($productRepository->find($id));

        return new Response('<html><body></body></html>');
    }

    /**
     * @Route("/create")
     * @return Response
     */
    public function create()
    {
        $product = new Product();
        $product->setName("苹果");
        $product->setPrice(12.2);
        $product->setCategoryId(1);
        $em = $this->getDoctrine()->getManager();
        $em->persist($product);

        $em->flush();
        dump($product);
        return new Response('<html><body></body></html>');
    }

    /**
     * @Route("/update/{product_id}", requirements={"product_id":"\d+"})
     * @param int $product_id
     * @param ProductRepository $productRepository
     * @return Response
     */
    public function update(int $product_id, ProductRepository $productRepository)
    {
        $em = $this->getDoctrine()->getManager();

//        /** @var Product $product */
        $product = $productRepository->find($product_id);
        $product->setPrice(22552.25);
        $em->persist($product);
        $em->flush();

        dump($product);

        return new Response('<html><body></body></html>');
    }

    /**
     * @Route("/remove/{product_id}", requirements={"product_id":"\d+"})
     *
     * @param int $product_id
     * @return Response
     */
    public function delete(int $product_id)
    {
        $em = $this->getDoctrine()->getManager();
        $product = $em->getRepository(Product::class)->find($product_id);

//        $em->getFilters()->enable("soft-deleteable");

//        dd($em->getFilters());

        $em->remove($product);
        $em->flush();

        return new Response('<html><body></body></html>');
    }
}
